<?php

// +----------------------------------------------------------------------

// | OneThink [ WE CAN DO IT JUST THINK IT ]

// +----------------------------------------------------------------------

// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.

// +----------------------------------------------------------------------

// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>

// +----------------------------------------------------------------------



namespace Home\Controller;

use Think\Controller;



/**

 * 会员控制器

 */

class MycourseController extends HomeController{

	/**

	 * [course description]展示想当学生和相当老师的方法

	 * @return [type] [description]

	 */

	public function course(){

		$id	   = $_SESSION['onethink_home']['uid'];

		//查找想要教的

		$data  = D('Mycourse') ->showAll($id);

		//查找想要学的

		$data2 = D('Mycourse') ->showStudent($id);

		$this ->assign('data',$data);

		$this ->assign('data2',$data2);

		$this ->display();

	}

	/**

	 * [myStudent description]查找教学生的老师的信息

	 * @return [type] [description]

	 */

	public function myStudent(){

		$uid  = I('get.uid');

		$cid  = I('get.cid');

		$order = I('get.order');
		$CourseEva = D('CourseEva') ->where(array('order' =>$order)) ->find();

		if($CourseEva){

			$this ->redirect('Course/showAssess',array('cid'=>$cid,'order'=>$order));

		}else{

			//查找确认

			$quanren = D('CourseRecord') ->quanren($order);
	
			$this ->assign('quanren',$quanren);

			//查找出指定的UID和班级ID的学生

			$data = D('Mycourse') ->selectOne($uid,$cid,$order);
// dump($data);die;
			//去掉整数后的.00

			$data['salary'] = ($data['salary'] == (int)$data['salary']) ? (int)$data['salary'] : $data['salary'];

			$data['area_id2'] = getAreaId($data['area_id']);

			$this ->assign('data',$data);

			//查找下节课的上课时间

			$nextTime = D('CourseRecord') ->nextTime($uid,$cid);

			$this ->assign('nextTime',$nextTime);

			//取出本用户的ID

			//查找所有上课的时间;

			$time = D('CourseRecord') ->showTime($data['user_id'],$data['m_cid'],$order);

			$this ->assign('time',$time);

			//查询课程是否以上完

			$sure = D('Mycourse') ->compare($time);

			$this ->assign('sure',$sure);

			//当前时间戳

			$date = time();

			$this ->assign('date',$date);

			//取出课时

			$num = D('Mycourse') ->showTimeNum($time,$date);

			$this ->assign('num',$num);

			//查询大于当前时间的课程有没有

			$timeOne = D('Mycourse') ->showTimeOne($uid,$cid);

			$this ->assign('timeOne',$timeOne);

			//获取最近的没有确认的课程

			// $t = D('Mycourse') ->isCourse($uid,$cid);

			$t = D('Mycourse') ->isCourse($order);

			//剩余的天数

			$d = floor($t / (3600 * 24));

			$this ->assign('d',$d);

			//剩余的小时数

			$h = floor(($t % (3600 * 24))/3600);

			//检查是否退订的

			$cancel = D('Mycourse') ->isCancel($uid,$cid);

			$this ->assign('cancel',$cancel);

			$this ->assign('h',$h);

			//查找是否所有课程都已经却认完成

			$affAll = D('CourseRecord') ->affAll($uid,$cid);

			$this ->assign('affAll',$affAll);

			//查看是否上完课程

			$class_ok = D('CourseRecord') ->class_ok($uid,$cid,$order);

			$this ->assign('class_ok',$class_ok);

			//把订单号传过去

			$this ->assign('order',$order);

			$this ->display();

		}

	}

	/**

	 * ajax老师发布时间

	 * @return [type] [description]

	 */

	public function mystuAjax(){

		$post = I('post.');

		D('CourseRecord') ->stuMsg($post['id']);

		$data = D('Mycourse') ->mystuAjax($post);

		$this ->ajaxReturn($data);

	}

	/**

	 * 学生确认的ajax

	 * @return [type] [description]

	 */

	public function mystuAjaxStu(){

	    $order = I('post.order');

	    //判断是否大于一节课

	    // $gearr = D('Mycourse') ->geArr($ids);

	    // //如果返回1就是选了多节课

	    // if($gearr == 1){

	    // 	$this ->ajaxReturn('不能一次确认多节课');

	    // }

	    //修改确认课程以后修改钱

	    // $rid = $ids[0];.

	    $result = D('CourseRecord') ->orderTime($order);

	    if($result){

		    foreach ($result as $key => $value) {

		    	 D('Mycourse') ->updateSalary($value['id'],$order);

		   		 D('Mycourse') ->changeStudents($value['id']);

				 D('Mycourse') ->mystuAjaxStu($value['id']);

		    }

			//修改教过的学生个数

			$this ->ajaxReturn("课程确认成功");

		}else{

			$this ->ajaxReturn("未到上课时间");

		}

	}

}